home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / histogram / test.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-18  |  10.1 KB  |  485 lines

  1. /* histogram/test.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <gsl/gsl_histogram.h>
  24. #include <gsl/gsl_test.h>
  25. #include <gsl/gsl_ieee_utils.h>
  26.  
  27. #define N 397
  28. #define NR 10
  29.  
  30. int
  31. main (void)
  32. {
  33.   double xr[NR + 1] =
  34.   {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
  35.  
  36.   gsl_histogram *h, *h1, *hr, *g;
  37.   size_t i, j;
  38.  
  39.   gsl_ieee_env_setup ();
  40.  
  41.   h = gsl_histogram_calloc (N);
  42.   h1 = gsl_histogram_calloc (N);
  43.   g = gsl_histogram_calloc (N);
  44.  
  45.   gsl_test (h->range == 0, "gsl_histogram_alloc returns valid range pointer");
  46.   gsl_test (h->bin == 0, "gsl_histogram_alloc returns valid bin pointer");
  47.   gsl_test (h->n != N, "gsl_histogram_alloc returns valid size");
  48.  
  49.  
  50.   hr = gsl_histogram_calloc_range (NR, xr);
  51.  
  52.   gsl_test (hr->range == 0, "gsl_histogram_calloc_range returns valid range pointer");
  53.   gsl_test (hr->bin == 0, "gsl_histogram_calloc_range returns valid bin pointer");
  54.   gsl_test (hr->n != NR, "gsl_histogram_calloc_range returns valid size");
  55.  
  56.   {
  57.     int status = 0;
  58.     for (i = 0; i <= NR; i++)
  59.       {
  60.     if (hr->range[i] != xr[i])
  61.       {
  62.         status = 1;
  63.       }
  64.       };
  65.  
  66.     gsl_test (status, "gsl_histogram_calloc_range creates range correctly");
  67.   }
  68.  
  69.   for (i = 0; i <= NR; i++)
  70.     {
  71.       hr->range[i] = 0.0;
  72.     }
  73.  
  74.   {
  75.     int status = gsl_histogram_set_ranges (hr, xr, NR+1);
  76.  
  77.     for (i = 0; i <= NR; i++)
  78.       {
  79.     if (hr->range[i] != xr[i])
  80.       {
  81.         status = 1;
  82.       }
  83.       };
  84.  
  85.     gsl_test (status, "gsl_histogram_set_range sets range correctly");
  86.   }
  87.     
  88.  
  89.   for (i = 0; i < N; i++)
  90.     {
  91.       gsl_histogram_accumulate (h, (double) i, (double) i);
  92.     };
  93.  
  94.   {
  95.     int status = 0;
  96.  
  97.     for (i = 0; i < N; i++)
  98.       {
  99.     if (h->bin[i] != (double) i)
  100.       {
  101.         status = 1;
  102.       }
  103.       };
  104.  
  105.     gsl_test (status, "gsl_histogram_accumulate writes into array correctly");
  106.   }
  107.  
  108.  
  109.   {
  110.     int status = 0;
  111.  
  112.     for (i = 0; i < N; i++)
  113.       {
  114.     if (gsl_histogram_get (h, i) != i)
  115.       status = 1;
  116.       };
  117.     gsl_test (status, "gsl_histogram_get reads from array correctly");
  118.   }
  119.  
  120.   for (i = 0; i <= N; i++)
  121.     {
  122.       h1->range[i] = 100.0 + i;
  123.     }
  124.  
  125.   gsl_histogram_memcpy (h1, h);
  126.  
  127.   {
  128.     int status = 0;
  129.     for (i = 0; i <= N; i++)
  130.       {
  131.     if (h1->range[i] != h->range[i])
  132.       status = 1;
  133.       };
  134.     gsl_test (status, "gsl_histogram_memcpy copies bin ranges correctly");
  135.   }
  136.  
  137.   {
  138.     int status = 0;
  139.     for (i = 0; i < N; i++)
  140.       {
  141.     if (gsl_histogram_get (h1, i) != gsl_histogram_get (h, i))
  142.       status = 1;
  143.       };
  144.     gsl_test (status, "gsl_histogram_memcpy copies bin values correctly");
  145.   }
  146.  
  147.   gsl_histogram_free (h1);
  148.  
  149.   h1 = gsl_histogram_clone (h);
  150.  
  151.   {
  152.     int status = 0;
  153.     for (i = 0; i <= N; i++)
  154.       {
  155.     if (h1->range[i] != h->range[i])
  156.       status = 1;
  157.       };
  158.     gsl_test (status, "gsl_histogram_clone copies bin ranges correctly");
  159.   }
  160.  
  161.   {
  162.     int status = 0;
  163.     for (i = 0; i < N; i++)
  164.       {
  165.     if (gsl_histogram_get (h1, i) != gsl_histogram_get (h, i))
  166.       status = 1;
  167.       };
  168.     gsl_test (status, "gsl_histogram_clone copies bin values correctly");
  169.   }
  170.  
  171.   gsl_histogram_reset (h);
  172.  
  173.   {
  174.     int status = 0;
  175.  
  176.     for (i = 0; i < N; i++)
  177.       {
  178.     if (h->bin[i] != 0)
  179.       status = 1;
  180.       }
  181.     gsl_test (status, "gsl_histogram_reset zeros array correctly");
  182.   }
  183.  
  184.  
  185.   {
  186.     int status = 0;
  187.  
  188.     for (i = 0; i < N; i++)
  189.       {
  190.     gsl_histogram_increment (h, (double) i);
  191.  
  192.     for (j = 0; j <= i; j++)
  193.       {
  194.         if (h->bin[j] != 1)
  195.           {
  196.         status = 1;
  197.           }
  198.       }
  199.  
  200.     for (j = i + 1; j < N; j++)
  201.       {
  202.         if (h->bin[j] != 0)
  203.           {
  204.         status = 1;
  205.           }
  206.       }
  207.       }
  208.  
  209.     gsl_test (status, "gsl_histogram_increment works correctly");
  210.   }
  211.  
  212.   {
  213.     int status = 0;
  214.     for (i = 0; i < N; i++)
  215.       {
  216.     double x0 = 0, x1 = 0;
  217.  
  218.     gsl_histogram_get_range (h, i, &x0, &x1);
  219.  
  220.     if (x0 != i || x1 != i + 1)
  221.       {
  222.         status = 1;
  223.       }
  224.       }
  225.     gsl_test (status, "gsl_histogram_getbinrange works correctly");
  226.   }
  227.  
  228.   {
  229.     int status = 0;
  230.     if (gsl_histogram_max (h) != N)
  231.       status = 1;
  232.     gsl_test (status, "gsl_histogram_max works correctly");
  233.   }
  234.  
  235.   {
  236.     int status = 0;
  237.     if (gsl_histogram_min (h) != 0)
  238.       status = 1;
  239.     gsl_test (status, "gsl_histogram_min works correctly");
  240.   }
  241.  
  242.   {
  243.     int status = 0;
  244.     if (gsl_histogram_bins (h) != N)
  245.       status = 1;
  246.     gsl_test (status, "gsl_histogram_bins works correctly");
  247.   }
  248.  
  249.   h->bin[2] = 123456.0;
  250.   h->bin[4] = -654321;
  251.  
  252.   {
  253.     double max = gsl_histogram_max_val (h);
  254.     gsl_test (max != 123456.0, "gsl_histogram_max_val finds maximum value");
  255.   }
  256.  
  257.   {
  258.     double min = gsl_histogram_min_val (h);
  259.     gsl_test (min != -654321.0, "gsl_histogram_min_val finds minimum value");
  260.   }
  261.  
  262.   {
  263.     size_t imax = gsl_histogram_max_bin (h);
  264.     gsl_test (imax != 2, "gsl_histogram_max_bin finds maximum value bin");
  265.   }
  266.  
  267.   {
  268.     size_t imin = gsl_histogram_min_bin (h);
  269.     gsl_test (imin != 4, "gsl_histogram_min_bin find minimum value bin");
  270.   }
  271.  
  272.   for (i = 0; i < N; i++)
  273.     {
  274.       h->bin[i] = i + 27;
  275.       g->bin[i] = (i + 27) * (i + 1);
  276.     }
  277.  
  278.   {
  279.     double sum=gsl_histogram_sum (h);
  280.     gsl_test(sum != N*27+((N-1)*N)/2, "gsl_histogram_sum sum all bin values");
  281.   }
  282.  
  283.   gsl_histogram_memcpy (h1, g);
  284.   gsl_histogram_add (h1, h);
  285.  
  286.   {
  287.     int status = 0;
  288.     for (i = 0; i < N; i++)
  289.       {
  290.     if (h1->bin[i] != g->bin[i] + h->bin[i])
  291.       status = 1;
  292.       }
  293.     gsl_test (status, "gsl_histogram_add works correctly");
  294.   }
  295.  
  296.   gsl_histogram_memcpy (h1, g);
  297.   gsl_histogram_sub (h1, h);
  298.  
  299.   {
  300.     int status = 0;
  301.     for (i = 0; i < N; i++)
  302.       {
  303.     if (h1->bin[i] != g->bin[i] - h->bin[i])
  304.       status = 1;
  305.       }
  306.     gsl_test (status, "gsl_histogram_sub works correctly");
  307.   }
  308.  
  309.  
  310.   gsl_histogram_memcpy (h1, g);
  311.   gsl_histogram_mul (h1, h);
  312.  
  313.   {
  314.     int status = 0;
  315.     for (i = 0; i < N; i++)
  316.       {
  317.     if (h1->bin[i] != g->bin[i] * h->bin[i])
  318.       status = 1;
  319.       }
  320.     gsl_test (status, "gsl_histogram_mul works correctly");
  321.   }
  322.  
  323.   gsl_histogram_memcpy (h1, g);
  324.   gsl_histogram_div (h1, h);
  325.  
  326.   {
  327.     int status = 0;
  328.     for (i = 0; i < N; i++)
  329.       {
  330.     if (h1->bin[i] != g->bin[i] / h->bin[i])
  331.       status = 1;
  332.       }
  333.     gsl_test (status, "gsl_histogram_div works correctly");
  334.   }
  335.  
  336.   gsl_histogram_memcpy (h1, g);
  337.   gsl_histogram_scale (h1, 0.5);
  338.  
  339.   {
  340.     int status = 0;
  341.     for (i = 0; i < N; i++)
  342.       {
  343.     if (h1->bin[i] != 0.5 * g->bin[i])
  344.       status = 1;
  345.       }
  346.     gsl_test (status, "gsl_histogram_scale works correctly");
  347.   }
  348.  
  349.   gsl_histogram_memcpy (h1, g);
  350.   gsl_histogram_shift (h1, 0.25);
  351.  
  352.   {
  353.     int status = 0;
  354.     for (i = 0; i < N; i++)
  355.       {
  356.     if (h1->bin[i] != 0.25 + g->bin[i])
  357.       status = 1;
  358.       }
  359.     gsl_test (status, "gsl_histogram_shift works correctly");
  360.   }
  361.  
  362.  
  363.   gsl_histogram_free (h);    /* free whatever is in h */
  364.  
  365.   h = gsl_histogram_calloc_uniform (N, 0.0, 1.0);
  366.  
  367.   gsl_test (h->range == 0,
  368.         "gsl_histogram_calloc_uniform returns valid range pointer");
  369.   gsl_test (h->bin == 0,
  370.         "gsl_histogram_calloc_uniform returns valid bin pointer");
  371.   gsl_test (h->n != N,
  372.         "gsl_histogram_calloc_uniform returns valid size");
  373.  
  374.   gsl_histogram_accumulate (h, 0.0, 1.0);
  375.   gsl_histogram_accumulate (h, 0.1, 2.0);
  376.   gsl_histogram_accumulate (h, 0.2, 3.0);
  377.   gsl_histogram_accumulate (h, 0.3, 4.0);
  378.  
  379.   {
  380.     size_t i1, i2, i3, i4;
  381.     double expected;
  382.     int status = gsl_histogram_find (h, 0.0, &i1);
  383.     status = gsl_histogram_find (h, 0.1, &i2);
  384.     status = gsl_histogram_find (h, 0.2, &i3);
  385.     status = gsl_histogram_find (h, 0.3, &i4);
  386.  
  387.     for (i = 0; i < N; i++)
  388.       {
  389.     if (i == i1)
  390.       {
  391.         expected = 1.0;
  392.       }
  393.     else if (i == i2)
  394.       {
  395.         expected = 2.0;
  396.       }
  397.     else if (i == i3)
  398.       {
  399.         expected = 3.0;
  400.       }
  401.     else if (i == i4)
  402.       {
  403.         expected = 4.0;
  404.       }
  405.     else
  406.       {
  407.         expected = 0.0;
  408.       }
  409.  
  410.     if (h->bin[i] != expected)
  411.       {
  412.         status = 1;
  413.       }
  414.  
  415.       }
  416.     gsl_test (status, "gsl_histogram_find works correctly");
  417.   }
  418.  
  419.  
  420.   {
  421.     FILE *f = fopen ("test.txt", "w");
  422.     gsl_histogram_fprintf (f, h, "%.19g", "%.19g");
  423.     fclose (f);
  424.   }
  425.  
  426.   {
  427.     FILE *f = fopen ("test.txt", "r");
  428.     gsl_histogram *hh = gsl_histogram_calloc (N);
  429.     int status = 0;
  430.  
  431.     gsl_histogram_fscanf (f, hh);
  432.  
  433.     for (i = 0; i < N; i++)
  434.       {
  435.     if (h->range[i] != hh->range[i])
  436.       status = 1;
  437.     if (h->bin[i] != hh->bin[i])
  438.       status = 1;
  439.       }
  440.     if (h->range[N] != hh->range[N])
  441.       status = 1;
  442.  
  443.     gsl_test (status, "gsl_histogram_fprintf and fscanf work correctly");
  444.  
  445.     gsl_histogram_free (hh);
  446.     fclose (f);
  447.   }
  448.  
  449.   {
  450.     FILE *f = fopen ("test.dat", "wb");
  451.     gsl_histogram_fwrite (f, h);
  452.     fclose (f);
  453.   }
  454.  
  455.   {
  456.     FILE *f = fopen ("test.dat", "rb");
  457.     gsl_histogram *hh = gsl_histogram_calloc (N);
  458.     int status = 0;
  459.  
  460.     gsl_histogram_fread (f, hh);
  461.  
  462.     for (i = 0; i < N; i++)
  463.       {
  464.     if (h->range[i] != hh->range[i])
  465.       status = 1;
  466.     if (h->bin[i] != hh->bin[i])
  467.       status = 1;
  468.       }
  469.     if (h->range[N] != hh->range[N])
  470.       status = 1;
  471.  
  472.     gsl_test (status, "gsl_histogram_fwrite and fread work correctly");
  473.  
  474.     gsl_histogram_free (hh);
  475.     fclose (f);
  476.   }
  477.  
  478.   gsl_histogram_free (h);
  479.   gsl_histogram_free (g);
  480.   gsl_histogram_free (h1);
  481.   gsl_histogram_free (hr);
  482.  
  483.   exit (gsl_test_summary ());
  484. }
  485.